﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;

namespace TestDxfDocument
{
    class Integral_T_slot_end_mills
    {
        public static void IntegralTslotendmills(Dictionary<string, Object> data)  //整体T形槽立铣刀
        {
            DxfDocument dxf = new DxfDocument();
            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer dimensioning = new Layer("dimensioning");    //尺寸层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中心线
            Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线
            Layer cutting = new Layer("cutting") { Color = AciColor.Red };    //切削层
            Layer nocutting = new Layer("nocutting");    //非切削层
            Layer skcontour = new Layer("skcontour") { Color = AciColor.Cyan };    //sk轮廓层
            Layer skdimensioning = new Layer("skdimensioning");    //sk尺寸线层
            Layer skrefin = new Layer("skrefin") { Color = AciColor.Red };    //sk基准线/内轮廓
            Layer sktext = new Layer("sktext") { Color = AciColor.Blue };  //sk文本
            Layer skhatching = new Layer("skhatching") { Color = AciColor.Blue };    //sk填充
            Layer skadditionlline = new Layer("skadditionlline") { Color = AciColor.Cyan };    //sk附加线
            Layer skviewcontour = new Layer("skviewcontour") { Color = AciColor.Cyan };    //skview轮廓
            Layer skviewdimensioning = new Layer("skviewdimensioning");    //skview尺寸线层

            //基本参数-------------------------------------------------------------------------------------------------
            double pi = Math.PI;
            string path = Convert.ToString(data["path"]);

            //赋值参数--------------------------------------------------------------------------------------------------
            /*double OAL = 82.37;
            double scale = OAL / 82.37;//放大系数
            double LPR = 45.51 * scale;
            double DCONMS = 24.08 * scale;
            double DC = 56.45 * scale;
            double LS = 46.79 * scale;
            double CW = 11.25 * scale;
            double DN = 21.87 * scale;
            double FHA = 19;
            double KAPR = 90;*/

            double OAL = Convert.ToDouble(data["oal"]);
            double LPR = Convert.ToDouble(data["lpr"]);
            double DCONMS = Convert.ToDouble(data["dconms"]);
            double DC = Convert.ToDouble(data["dc"]);
            double LS = Convert.ToDouble(data["ls"]);
            double CW = Convert.ToDouble(data["cw"]);
            double DN = Convert.ToDouble(data["dn"]);
            double FHA = Convert.ToDouble(data["fha"]);
            double KAPR = Convert.ToDouble(data["kapr"]);
            double scale = Convert.ToDouble(data["oal"]) / 82.37;//放大系数

            //点---------------------------------------------------------------------------------------------
            //第一部分
            //中心线点(1-4)
            Vector2 v1 = new Vector2(0, 0);
            Vector2 v2 = new Vector2(v1.X + OAL, v1.Y);
            Vector2 v3 = new Vector2(v2.X - LPR, v2.Y);

            //虚线
            Vector2 v4 = new Vector2(v3.X, v3.Y + 17.02 * scale);
            Vector2 v5 = fun.Polar(v4, 135 * pi / 180, 3.16);
            Vector2 v6 = new Vector2(v5.X - 7.54 * scale, v5.Y);
            Vector2 v7 = fun.mirrorpoint(v4, v1, v2);
            Vector2 v8 = fun.mirrorpoint(v5, v1, v2);
            Vector2 v9 = fun.mirrorpoint(v6, v1, v2);

            //轮廓
            Vector2 v10 = new Vector2(v1.X, v1.Y + DN / 2.0);
            Vector2 v11 = fun.Inters(v10, fun.Polar(v10, 45 * pi / 180, OAL), new Vector2(v1.X, v1.Y + DCONMS / 2.0), new Vector2(v1.X + OAL, v1.Y + DCONMS / 2.0));
            Vector2 v12 = new Vector2(v1.X + LS, v11.Y);
            Vector2 v13= fun.Inters(v12, fun.Polar(v12, -45 * pi / 180, OAL), new Vector2(v1.X, v1.Y + DN / 2.0), new Vector2(v1.X + OAL, v1.Y + DN / 2.0));
            Vector2 v14 = new Vector2(v13.X + 21.28 * scale, v13.Y);
            Vector2 v15 = fun.Polar(v14, 28 * pi / 180, (4.76 / 11.25) * CW);
            Vector2 v10_1 = fun.mirrorpoint(v10, v1, v2);
            Vector2 v11_1 = fun.mirrorpoint(v11, v1, v2);
            Vector2 v12_1 = fun.mirrorpoint(v12, v1, v2);
            Vector2 v13_1 = fun.mirrorpoint(v13, v1, v2);
            Vector2 v14_1 = fun.mirrorpoint(v14, v1, v2);
            Vector2 v16 = fun.Polar(v15, 171 * pi / 180, (1.78 / 11.25) * CW);
            Vector2 v17 = fun.Polar(v16, 99 * pi / 180, (4.87 / 56.45) * DC);
            Vector2 v18 = fun.Inters(v15, fun.Polar(v15, 90 * pi / 180, OAL), v17, fun.Polar(v17, -9 * pi / 180, OAL));
            Vector2 v19 = new Vector2(v18.X, v18.Y + (6.56 / 56.45) * DC);
            Vector2 v20 = new Vector2(v19.X - (1.45 / 11.25) * CW, v19.Y);
            Vector2 v21 = fun.Polar(v20, 108 * pi / 180, (3.87 / 56.45) * DC);
            Vector2 v22 = new Vector2(v21.X + (9.94 / 11.25) * CW, v21.Y);
            Vector2 v23 = new Vector2(v21.X + CW, v20.Y);
            Vector2 v24 = new Vector2(v23.X - (0.74 / 11.25) * CW, v23.Y);
            Vector2 v25 = fun.Inters(v17, fun.Polar(v17, -9 * pi / 180, OAL), v24, fun.Polar(v24, -90 * pi / 180, OAL));
            Vector2 v26 = fun.Polar(v16, -9 * pi / 180, (9.9 / 11.25) * CW);
            Vector2 v27 = fun.Inters(v16, fun.Polar(v16, -9 * pi / 180, OAL), v24, fun.Polar(v24, -90 * pi / 180, OAL));
            Vector2 v28 = fun.Polar(v26, -90 * pi / 180, (10.31 / 56.45) * DC);
            Vector2 v29 = fun.Polar(v28, -90 * pi / 180, (4.93 / 56.45) * DC);
            Vector2 v30 = fun.Polar(v29, -90 * pi / 180, (9.5 / 56.45) * DC);
            Vector2 v31 = fun.Polar(v30, -81 * pi / 180, (4.87 / 56.45) * DC);
            Vector2 v32 = fun.Inters(v31, fun.Polar(v31, 171 * pi / 180, OAL), v24, fun.Polar(v24, -90 * pi / 180, OAL));
            Vector2 v33 = new Vector2(v32.X, v32.Y - (7.11 / 56.45) * DC);
            Vector2 v34 = new Vector2(v21.X + CW, v22.Y - DC);
            Vector2 v35 = new Vector2(v20.X, v34.Y);
            Vector2 v36 = new Vector2(v21.X, v33.Y);
            Vector2 v37 = new Vector2(v19.X, v33.Y);
            Vector2 v38 = fun.Inters(v31, fun.Polar(v31, 171 * pi / 180, OAL), v19, fun.Polar(v19, -90 * pi / 180, OAL));
            Vector2 v39 = fun.Polar(v38, 171 * pi / 180, (1.78 / 11.25) * CW);
            Vector2 v40 = fun.Polar(v39, 99 * pi / 180, (4.87 / 56.45) * DC);
            Vector2 v41 = fun.Inters(v30, fun.Polar(v30, 171 * pi / 180, OAL), v19, fun.Polar(v19, -90 * pi / 180, OAL));
            Vector2 v42 = fun.Inters(v29, fun.Polar(v29, 162 * pi / 180, OAL), v19, fun.Polar(v19, -90 * pi / 180, OAL));
            Vector2 v43 = fun.Polar(v42, 162 * pi / 180, (1.33 / 11.25) * CW);
            Vector2 v45 = fun.Inters(v28, fun.Polar(v28, 162 * pi / 180, OAL), v19, fun.Polar(v19, -90 * pi / 180, OAL));
            Vector2 v44 = fun.Polar(v45, 162 * pi / 180, (1.43 / 11.25) * CW);

            //作图部分-----------------------------------------------------------------------
            //第一部分
            //中心线
            Line centre_line1 = new Line(v1, v2);
            centre_line1.Layer = centre;
            centre_line1.Linetype = Linetype.Center;
            centre_line1.LinetypeScale = 5;
            dxf.AddEntity(centre_line1);

            //虚线
            LwPolyline polyline1 = new LwPolyline();
            polyline1.Vertexes.Add(new LwPolylineVertex(v6));
            polyline1.Vertexes.Add(new LwPolylineVertex(v5));
            polyline1.Vertexes.Add(new LwPolylineVertex(v4));
            polyline1.Vertexes.Add(new LwPolylineVertex(v7));
            polyline1.Vertexes.Add(new LwPolylineVertex(v8));
            polyline1.Vertexes.Add(new LwPolylineVertex(v9));
            polyline1.Layer = refin;
            polyline1.Linetype = Linetype.Dashed;
            polyline1.LinetypeScale = 5;
            dxf.AddEntity(polyline1);

            //轮廓
            LwPolyline polyline2 = new LwPolyline();
            polyline2.Vertexes.Add(new LwPolylineVertex(v15));
            polyline2.Vertexes.Add(new LwPolylineVertex(v14));//0
            polyline2.Vertexes.Add(new LwPolylineVertex(v13));//1
            polyline2.Vertexes.Add(new LwPolylineVertex(v12));//2
            polyline2.Vertexes.Add(new LwPolylineVertex(v11));//3
            polyline2.Vertexes.Add(new LwPolylineVertex(v10));//4
            polyline2.Vertexes.Add(new LwPolylineVertex(v10_1));//5
            polyline2.Vertexes.Add(new LwPolylineVertex(v11_1));//6
            polyline2.Vertexes.Add(new LwPolylineVertex(v12_1));//7
            polyline2.Vertexes.Add(new LwPolylineVertex(v13_1));//8
            polyline2.Vertexes.Add(new LwPolylineVertex(v14_1));//9
            polyline2.Vertexes.Add(new LwPolylineVertex(v41));//10
            polyline2.Vertexes[0].Bulge = Math.Tan(-75 * pi / 180 / 4);
            polyline2.Vertexes[10].Bulge = Math.Tan(-75 * pi / 180 / 4);
            fun.PolyFill(dxf, polyline2, 0.3, contour);

            LwPolyline polyline3 = new LwPolyline();
            polyline3.Vertexes.Add(new LwPolylineVertex(v11));
            polyline3.Vertexes.Add(new LwPolylineVertex(v11_1));
            fun.PolyFill(dxf, polyline3, 0.3, contour);

            LwPolyline polyline4 = new LwPolyline();
            polyline4.Vertexes.Add(new LwPolylineVertex(v12));
            polyline4.Vertexes.Add(new LwPolylineVertex(v12_1));
            fun.PolyFill(dxf, polyline4, 0.3, contour);

            LwPolyline polyline5 = new LwPolyline();
            polyline5.Vertexes.Add(new LwPolylineVertex(v13));
            polyline5.Vertexes.Add(new LwPolylineVertex(v13_1));
            fun.PolyFill(dxf, polyline5, 0.3, contour);

            LwPolyline polyline6 = new LwPolyline();
            polyline6.Vertexes.Add(new LwPolylineVertex(v14));
            polyline6.Vertexes.Add(new LwPolylineVertex(v14_1));
            fun.PolyFill(dxf, polyline6, 0.3, contour);

            LwPolyline polyline7 = new LwPolyline();
            polyline7.Vertexes.Add(new LwPolylineVertex(v15));
            polyline7.Vertexes.Add(new LwPolylineVertex(v16));//0
            polyline7.Vertexes.Add(new LwPolylineVertex(v17));//1
            polyline7.Vertexes.Add(new LwPolylineVertex(v18));//2
            polyline7.Vertexes.Add(new LwPolylineVertex(v19));//3
            polyline7.Vertexes.Add(new LwPolylineVertex(v20));//4
            polyline7.Vertexes.Add(new LwPolylineVertex(v21));//5
            polyline7.Vertexes.Add(new LwPolylineVertex(v22));//6
            polyline7.Vertexes.Add(new LwPolylineVertex(v23));//7
            polyline7.Vertexes.Add(new LwPolylineVertex(v24));//8
            polyline7.Vertexes.Add(new LwPolylineVertex(v25));//9
            polyline7.Vertexes.Add(new LwPolylineVertex(v26));//10
            polyline7.Vertexes.Add(new LwPolylineVertex(v27));//11
            polyline7.Vertexes.Add(new LwPolylineVertex(v28));//12
            polyline7.Vertexes.Add(new LwPolylineVertex(v29));//13
            polyline7.Vertexes.Add(new LwPolylineVertex(v30));//14
            polyline7.Vertexes.Add(new LwPolylineVertex(v31));//15
            polyline7.Vertexes.Add(new LwPolylineVertex(v32));//16
            polyline7.Vertexes.Add(new LwPolylineVertex(v33));//17
            polyline7.Vertexes.Add(new LwPolylineVertex(v34));//18
            polyline7.Vertexes[1].Bulge = Math.Tan(-66 * pi / 180 / 4);
            polyline7.Vertexes[5].Bulge = Math.Tan(-71 * pi / 180 / 4);
            polyline7.Vertexes[7].Bulge = Math.Tan(-71 * pi / 180 / 4);
            polyline7.Vertexes[10].Bulge = Math.Tan(-66 * pi / 180 / 4);
            polyline7.Vertexes[13].Bulge = Math.Tan(-57 * pi / 180 / 4);
            polyline7.Vertexes[15].Bulge = Math.Tan(-66 * pi / 180 / 4);
            polyline7.Vertexes[18].Bulge = Math.Tan(-71 * pi / 180 / 4);
            fun.PolyFill(dxf, polyline7, 0.3, contour);

            LwPolyline polyline8 = new LwPolyline();
            polyline8.Vertexes.Add(new LwPolylineVertex(v35));
            polyline8.Vertexes.Add(new LwPolylineVertex(v36));//0
            polyline8.Vertexes.Add(new LwPolylineVertex(v37));//1
            polyline8.Vertexes.Add(new LwPolylineVertex(v38));//2
            polyline8.Vertexes.Add(new LwPolylineVertex(v39));//3
            polyline8.Vertexes.Add(new LwPolylineVertex(v40));//4
            polyline8.Vertexes.Add(new LwPolylineVertex(v41));//5
            polyline8.Vertexes.Add(new LwPolylineVertex(v42));//6
            polyline8.Vertexes.Add(new LwPolylineVertex(v43));//7
            polyline8.Vertexes.Add(new LwPolylineVertex(v44));//8
            polyline8.Vertexes.Add(new LwPolylineVertex(v45));//9
            polyline8.Vertexes.Add(new LwPolylineVertex(v15));//10
            polyline8.Vertexes[0].Bulge = Math.Tan(-71 * pi / 180 / 4);
            polyline8.Vertexes[4].Bulge = Math.Tan(-66 * pi / 180 / 4);
            polyline8.Vertexes[8].Bulge = Math.Tan(-57 * pi / 180 / 4);
            fun.PolyFill(dxf, polyline8, 0.3, contour);

            LwPolyline polyline9 = new LwPolyline();
            polyline9.Vertexes.Add(new LwPolylineVertex(v19));
            polyline9.Vertexes.Add(new LwPolylineVertex(v24));
            fun.PolyFill(dxf, polyline9, 0.3, contour);

            LwPolyline polyline10 = new LwPolyline();
            polyline10.Vertexes.Add(new LwPolylineVertex(v18));
            polyline10.Vertexes.Add(new LwPolylineVertex(v25));
            fun.PolyFill(dxf, polyline10, 0.3, contour);

            LwPolyline polyline11 = new LwPolyline();
            polyline11.Vertexes.Add(new LwPolylineVertex(v15));
            polyline11.Vertexes.Add(new LwPolylineVertex(v27));
            fun.PolyFill(dxf, polyline11, 0.3, contour);

            LwPolyline polyline12 = new LwPolyline();
            polyline12.Vertexes.Add(new LwPolylineVertex(v45));
            polyline12.Vertexes.Add(new LwPolylineVertex(v28));
            fun.PolyFill(dxf, polyline12, 0.3, contour);

            LwPolyline polyline13 = new LwPolyline();
            polyline13.Vertexes.Add(new LwPolylineVertex(v42));
            polyline13.Vertexes.Add(new LwPolylineVertex(v29));
            fun.PolyFill(dxf, polyline13, 0.3, contour);

            LwPolyline polyline14 = new LwPolyline();
            polyline14.Vertexes.Add(new LwPolylineVertex(v41));
            polyline14.Vertexes.Add(new LwPolylineVertex(v30));
            fun.PolyFill(dxf, polyline14, 0.3, contour);

            LwPolyline polyline15 = new LwPolyline();
            polyline15.Vertexes.Add(new LwPolylineVertex(v38));
            polyline15.Vertexes.Add(new LwPolylineVertex(v32));
            fun.PolyFill(dxf, polyline15, 0.3, contour);

            LwPolyline polyline16 = new LwPolyline();
            polyline16.Vertexes.Add(new LwPolylineVertex(v37));
            polyline16.Vertexes.Add(new LwPolylineVertex(v33));
            fun.PolyFill(dxf, polyline16, 0.3, contour);

            LwPolyline polyline17 = new LwPolyline();
            polyline17.Vertexes.Add(new LwPolylineVertex(v35));
            polyline17.Vertexes.Add(new LwPolylineVertex(v34));
            fun.PolyFill(dxf, polyline17, 0.3, contour);

            //标注---------------------------------------------------------------------------
            double height = 4 * scale;
            double arrowsize = 2 * scale;
            double exline = 0.5 * scale;

            //DCONMS
            fun.dimrot1(dxf, v11, v11_1, 90, CW * 1.2, 1, height, arrowsize, exline, "DCONMS" /*+ Math.Round(PDX, 2)*/, " ", " ");

            //OAL,LF
            fun.dimrot1(dxf, v10, v23, 0, DC, 0.5, height, arrowsize, exline, "OAL,LF" /*+ Math.Round(CW, 2)*/, "", "");

            //LPR,LTA
            fun.dimrot1(dxf, v4, v23, 0, DC * 0.7, 0.5, height, arrowsize, exline, "LPR,LTA" /*+ Math.Round(CW, 2)*/, "", "");

            //DC
            fun.dimrot1(dxf, v22, v34, -90, CW * 2, 0.5, height, arrowsize, exline, "DC" /*+ Math.Round(CW, 2)*/, "", "");

            //LS
            fun.dimrot1(dxf, v11_1, v12_1, 180, DC * 0.5, 0.5, height, arrowsize, exline, "LS" /*+ Math.Round(CW, 2)*/, "", "");

            //CW
            fun.dimrot1(dxf, v36, v34, 180, DC * 0.5 - (Math.Abs(v12_1.Y - Math.Abs((v36.Y - v34.Y) / 2.0))), 0.5, height, arrowsize, exline, "CW" /*+ Math.Round(CW, 2)*/, "", "");

            //DN
            fun.dimrot1(dxf, v13, v13_1, -90, CW * 0.2, 0.5, height, arrowsize, exline, "DN" /*+ Math.Round(CW, 2)*/, "", "");

            //CCMS
            fun.jt1(dxf, new Vector2(v12.X - LS * 0.3, v12.Y), 125, (19.33 / 56.45) * DC, 0, "CCMS", height);

            //FDP
            fun.jt1(dxf, fun.Polar(v22, 65 * pi / 180, (49.08 / 56.45) * DC), 270, (19.33 / 56.45) * DC, 0, "FDP", height);

            //坐标系
            Line line1 = new Line(v3, fun.Polar(v3, -136 * pi / 180, (21.57 / 56.45) * DC));
            dxf.AddEntity(line1);
            fun.text(dxf, fun.Polar(v3, -145 * pi / 180, (30 / 56.45) * DC), height, "MCS");
            fun.Axis(dxf, v3, 180, 270, "", "", "", 0.5 * scale, height, 0);
            fun.Axis(dxf, v2, 180, 270, "", "", "CIP", 0.5 * scale, height, 0);
            fun.Axis(dxf, v1, 180, 270, "", "", "PCS", 0.5 * scale, height, 0);
            fun.Axis(dxf, fun.Polar(v1, -105 * pi / 180, (39.45 / 56.45) * DC), 180, 270, "Z", "X", "Y", 0.5 * scale, height, 0);

            //角度标注
            //KAPR
            fun.jdbz2(dxf, new Line(v23, fun.Polar(v23, 90 * pi / 180, 0.1)), new Line(v21, fun.Polar(v21, 180 * pi / 180, 0.1)), DC * 0.3, height, arrowsize, exline, "KAPR", "", "");

            //FHA
            fun.jdbz2(dxf, new Line(v2, fun.Polar(v2, (180 - FHA) * pi / 180, 0.1)), new Line(v2, fun.Polar(v2, 180 * pi / 180, 0.1)), CW * 2, height, arrowsize, exline, "FHA", "", "");



            double isframe = Convert.ToInt32(data["isframe"]);
            String isframe_chose = Convert.ToString(data["isframe_chose"]);
            Vector2 p0 = new Vector2(0, 0);
            if (isframe == 1)
            {
                if (isframe_chose.Equals("0"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA0X(dxf, new Vector2(p0.X - 1189 / 2.0, p0.Y + 841 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("1"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA1X(dxf, new Vector2(p0.X - 841 / 2.0, p0.Y + 594 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("2"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA2X(dxf, new Vector2(p0.X - 594 / 2.0, p0.Y + 420 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("3"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA3X(dxf, new Vector2(p0.X - 420 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4X"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4X(dxf, new Vector2(p0.X - 297 / 2.0, p0.Y + 210 / 2.0 - 10 * 2.26), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4Y"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4Y(dxf, new Vector2(p0.X - 210 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }
            }

            //定义格式，根据路径导出
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);
        }
    }
}
